```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模板设计模式：定义算法的骨架</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .serif {
            font-family: 'Noto Serif SC', serif;
        }
        .drop-cap::first-letter {
            font-size: 3.5em;
            float: left;
            line-height: 0.8;
            margin-right: 0.1em;
            color: #667eea;
            font-weight: 700;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .code-block {
            background: #2d3748;
            border-left: 4px solid #667eea;
        }
        .transition-all {
            transition: all 0.3s ease;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:py-32">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0 md:pr-10">
                    <h1 class="serif text-4xl md:text-5xl font-bold leading-tight mb-4">模板设计模式</h1>
                    <h2 class="text-2xl md:text-3xl font-light mb-6">定义算法的骨架</h2>
                    <p class="text-lg opacity-90 mb-8">一种将算法结构固定而允许子步骤灵活变化的行为型设计模式，实现代码复用与扩展的完美平衡</p>
                    <div class="flex space-x-4">
                        <a href="#definition" class="bg-white text-indigo-700 hover:bg-indigo-100 px-6 py-3 rounded-lg font-medium transition-all">
                            <i class="fas fa-book mr-2"></i>了解模式
                        </a>
                        <a href="#example" class="bg-transparent border-2 border-white hover:bg-white hover:text-indigo-700 px-6 py-3 rounded-lg font-medium transition-all">
                            <i class="fas fa-code mr-2"></i>查看示例
                        </a>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <div class="bg-white bg-opacity-20 p-6 rounded-xl backdrop-blur-sm">
                        <div class="mermaid">
                            graph TD
                            A[抽象父类] -->|定义模板方法| B[固定算法步骤]
                            A --> C[抽象方法]
                            A --> D[钩子方法]
                            B --> E[调用抽象方法]
                            B --> F[调用钩子方法]
                            G[具体子类] -->|实现| C
                            G -->|可选重写| D
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Definition Section -->
        <section id="definition" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 w-12 mr-4"></div>
                <h2 class="serif text-3xl font-bold text-gray-800">模式定义</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md p-8 mb-8 transition-all card-hover">
                <p class="drop-cap text-gray-700 mb-6">模板设计模式是一种行为型设计模式，它定义了一个算法的骨架，将算法的具体实现延迟到子类中。模板方法定义了一个算法的步骤，其中一些步骤由父类实现，而其他步骤由子类实现。这种模式允许在不改变算法结构的情况下重新定义算法的某些步骤。</p>
                
                <div class="grid md:grid-cols-3 gap-6 mt-10">
                    <div class="bg-indigo-50 p-6 rounded-lg">
                        <div class="text-indigo-600 text-2xl mb-4">
                            <i class="fas fa-project-diagram"></i>
                        </div>
                        <h3 class="serif text-xl font-semibold mb-2 text-gray-800">定义算法骨架</h3>
                        <p class="text-gray-600">模板方法定义了一个算法的基本结构，将算法中的通用步骤放在父类中实现。</p>
                    </div>
                    
                    <div class="bg-purple-50 p-6 rounded-lg">
                        <div class="text-purple-600 text-2xl mb-4">
                            <i class="fas fa-code-branch"></i>
                        </div>
                        <h3 class="serif text-xl font-semibold mb-2 text-gray-800">子类实现细节</h3>
                        <p class="text-gray-600">模板方法中的一些步骤由父类实现，而其他具体步骤由子类实现。</p>
                    </div>
                    
                    <div class="bg-blue-50 p-6 rounded-lg">
                        <div class="text-blue-600 text-2xl mb-4">
                            <i class="fas fa-shield-alt"></i>
                        </div>
                        <h3 class="serif text-xl font-semibold mb-2 text-gray-800">开放封闭原则</h3>
                        <p class="text-gray-600">允许在不改变算法的结构的情况下，通过子类扩展来改变算法的某些步骤。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Use Cases Section -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 w-12 mr-4"></div>
                <h2 class="serif text-3xl font-bold text-gray-800">使用场景</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md p-8 transition-all card-hover">
                    <div class="text-indigo-500 text-3xl mb-4">
                        <i class="fas fa-sitemap"></i>
                    </div>
                    <h3 class="serif text-xl font-semibold mb-4 text-gray-800">算法骨架相同，步骤不同</h3>
                    <p class="text-gray-600">当多个算法有相同的结构但部分实现不同时，使用模板方法可以避免代码重复。父类定义算法框架，子类实现具体细节。</p>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-8 transition-all card-hover">
                    <div class="text-purple-500 text-3xl mb-4">
                        <i class="fas fa-cubes"></i>
                    </div>
                    <h3 class="serif text-xl font-semibold mb-4 text-gray-800">框架和库设计</h3>
                    <p class="text-gray-600">框架通常定义处理流程的骨架，而将具体实现留给应用开发者。如Java Servlet的service()方法和Spring的JdbcTemplate。</p>
                </div>
            </div>
            
            <div class="mt-8 bg-yellow-50 border-l-4 border-yellow-400 p-6 rounded-r-lg">
                <div class="flex">
                    <div class="text-yellow-500 text-xl mr-4">
                        <i class="fas fa-lightbulb"></i>
                    </div>
                    <div>
                        <h4 class="serif font-semibold text-gray-800 mb-2">设计启示</h4>
                        <p class="text-gray-600">识别算法中不变的部分和可变的部分，将不变的部分提升到父类，可变的部分留给子类实现。这符合"好莱坞原则"——"别调用我们，我们会调用你"。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Framework Examples -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 w-12 mr-4"></div>
                <h2 class="serif text-3xl font-bold text-gray-800">框架中的应用</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden transition-all card-hover">
                    <div class="bg-indigo-600 text-white p-6">
                        <h3 class="serif text-xl font-semibold mb-2">Java Servlet</h3>
                        <p class="opacity-90">service() 方法作为模板方法</p>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-600 mb-4">在Java Servlet中，<code class="bg-gray-100 px-2 py-1 rounded">service()</code>方法就是一个模板方法，它定义了Servlet处理HTTP请求的基本流程，包括请求解析、业务处理、响应生成等步骤。</p>
                        <p class="text-gray-600">开发者通过继承HttpServlet并重写<code class="bg-gray-100 px-2 py-1 rounded">doGet()</code>或<code class="bg-gray-100 px-2 py-1 rounded">doPost()</code>等方法来提供具体的业务逻辑实现。</p>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md overflow-hidden transition-all card-hover">
                    <div class="bg-purple-600 text-white p-6">
                        <h3 class="serif text-xl font-semibold mb-2">Spring JdbcTemplate</h3>
                        <p class="opacity-90">模板方法简化数据库操作</p>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-600 mb-4">Spring框架中的<code class="bg-gray-100 px-2 py-1 rounded">JdbcTemplate</code>类使用了模板方法模式，定义了数据库访问的基本骨架：获取连接、执行SQL、处理异常、释放资源等。</p>
                        <p class="text-gray-600">开发者只需提供SQL语句和结果处理逻辑，其他重复性工作都由模板方法处理，大大简化了JDBC编程。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Example -->
        <section id="example" class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 w-12 mr-4"></div>
                <h2 class="serif text-3xl font-bold text-gray-800">实现示例</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden transition-all card-hover">
                <div class="bg-gray-800 text-white p-4 flex justify-between items-center">
                    <h3 class="font-mono text-sm">烹饪流程模板方法示例</h3>
                    <span class="text-xs bg-gray-700 px-2 py-1 rounded">Java</span>
                </div>
                <div class="p-0 overflow-hidden">
                    <pre class="code-block text-gray-200 p-6 overflow-x-auto"><code>// 烹饪模板类
abstract class CookingTemplate {
    // 模板方法定义了烹饪流程
    public final void cook() {
        prepareIngredients();
        cookFood();
        serve();
    }

    // 抽象方法，由子类实现
    abstract void prepareIngredients();
    abstract void cookFood();

    // 钩子方法，可以有默认实现
    void serve() {
        System.out.println("Serving the dish.");
    }
}

// 烤肉类实现
class GrillCooking extends CookingTemplate {
    void prepareIngredients() {
        System.out.println("Preparing meat and spices.");
    }

    void cookFood() {
        System.out.println("Grilling the meat.");
    }
}

// 炒菜类实现
class StirFryCooking extends CookingTemplate {
    void prepareIngredients() {
        System.out.println("Chopping vegetables and seasonings.");
    }

    void cookFood() {
        System.out.println("Stir frying the vegetables.");
    }
}

// 客户端代码
public class TemplatePatternDemo {
    public static void main(String[] args) {
        CookingTemplate grillCooking = new GrillCooking();
        CookingTemplate stirFryCooking = new StirFryCooking();

        System.out.println("Preparing grilled dish:");
        grillCooking.cook();

        System.out.println("\nPreparing stir-fried dish:");
        stirFryCooking.cook();
    }
}</code></pre>
                </div>
            </div>
            
            <div class="mt-8 grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md p-6 transition-all card-hover">
                    <div class="text-green-500 text-2xl mb-4">
                        <i class="fas fa-check-circle"></i>
                    </div>
                    <h3 class="serif text-xl font-semibold mb-2 text-gray-800">模式优点</h3>
                    <ul class="text-gray-600 list-disc pl-5 space-y-2">
                        <li>提高代码复用性，将公共代码提取到父类</li>
                        <li>通过子类扩展实现算法可变部分，符合开闭原则</li>
                        <li>便于控制扩展点，父类可以控制子类的扩展方式</li>
                        <li>减少重复代码，提高可维护性</li>
                    </ul>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 transition-all card-hover">
                    <div class="text-red-500 text-2xl mb-4">
                        <i class="fas fa-exclamation-triangle"></i>
                    </div>
                    <h3 class="serif text-xl font-semibold mb-2 text-gray-800">注意事项</h3>
                    <ul class="text-gray-600 list-disc pl-5 space-y-2">
                        <li>模板方法应该是final的，防止子类重写整个算法</li>
                        <li>避免过多的抽象方法导致子类实现负担</li>
                        <li>合理使用钩子方法提供扩展点</li>
                        <li>注意模板方法与策略模式的区别</li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-1 bg-indigo-500 w-12 mr-4"></div>
                <h2 class="serif text-3xl font-bold text-gray-800">模式关系图</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-md p-8 transition-all card-hover">
                <div class="mermaid">
                    classDiagram
                    class AbstractClass {
                        +templateMethod()
                        #primitiveOperation1()
                        #primitiveOperation2()
                        +hook()
                    }
                    
                    class ConcreteClassA {
                        #primitiveOperation1()
                        #primitiveOperation2()
                    }
                    
                    class ConcreteClassB {
                        #primitiveOperation1()
                        #primitiveOperation2()
                        +hook()
                    }
                    
                    AbstractClass <|-- ConcreteClassA
                    AbstractClass <|-- ConcreteClassB
                    
                    note for AbstractClass "模板方法定义算法骨架\n调用原语操作和钩子方法"
                    note for ConcreteClassA "实现特定的原语操作"
                    note for ConcreteClassB "实现特定的原语操作\n并可选重写钩子方法"
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col items-center text-center">
                <h3 class="text-xl font-medium text-white mb-4">技术小馆</h3>
                <a href="http://www.yuque.com/jtostring" class="text-indigo-400 hover:text-indigo-300 transition-colors">
                    <i class="fas fa-globe mr-2"></i> http://www.yuque.com/jtostring
                </a>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            fontFamily: 'Noto Sans SC, sans-serif',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true
            },
            classDiagram: {
                useMaxWidth: true,
                defaultRenderer: 'dagre-d3'
            }
        });
        
        // 平滑滚动
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```